<?php
class User
{ 
  public $ID;
  public $firstname;
  public $lastname;
  public $email;

  function __construct($ID, $firstname, $lastname, $email)
  {
    $this->ID        = $ID;
    $this->firstname = $firstname;
    $this->lastname  = $lastname;
    $this->email     = $email;
  }
   
  public static function get_all()
  {
    return User::do_query("SELECT * FROM leden ORDER BY achternaam");
  }
 
  public static function get_distinct($day, $time)
  {
    $query = mysql_query('SELECT DISTINCT leden.ID FROM leden INNER JOIN '.DB_DEELNEMER.' INNER JOIN '.DB_LES.' ON ('.DB_DEELNEMER.'.les_ID = '.DB_LES.'.ID AND '.DB_DEELNEMER.'.user_ID = leden.ID) WHERE '.DB_LES.'.dag_naam="'.$day.'" AND '.DB_LES.'.tijd="'.$time.'" AND leden.vaste_klant=1');
    $affected = mysql_affected_rows();
    $users = array();

    if ($affected > 0)
      while ($row = mysql_fetch_assoc($query))
        $users[] = $row['ID'];

    return $users;
  }

  public static function get_user_id($section, $les_id, $order=False)
  {
    $order_by = $order ? "ORDER BY $order" : '';
    $query = mysql_query("SELECT user_ID FROM $section WHERE les_ID=$les_id $order_by");

    $users = array();

    while ($row = mysql_fetch_assoc($query))
      $users[] = $row['user_ID'];

    return $users;
  }

  public static function by_id($id)
  {  
    $query = 'SELECT * FROM leden WHERE ID="'.$id.'" LIMIT 1';
    return User::do_query($query, true);
  }

  public static function by_name($firstname, $lastname)
  {  
    $query = 'SELECT * FROM leden WHERE voornaam="'.$firstname.'" AND achternaam="'.$lastname.'" LIMIT 1';
    return User::do_query($query, true);
  }

  public static function by_email($firstname, $email)
  {  
    $query = 'SELECT * FROM leden WHERE voornaam="'.$firstname.'" AND email="'.$email.'" LIMIT 1';
    return User::do_query($query, true);
  }
  
  private function do_query($query, $single_user = false)
  {
    $users = array();     
    $result = mysql_query($query);
  
    while ($row = mysql_fetch_assoc($result))
    {
      $user = new User($row['ID'], $row['voornaam'], $row['achternaam'], $row['email']);

      if ($single_user)
        return $user;

      $users[] = $user;
    }
      
    return $users;
  }

  public static function add_user($firstname, $lastname, $email)
  {
    return mysql_query('INSERT INTO leden(voornaam, achternaam, email) VALUES ("'.$firstname.'", "'.$lastname.'", "'.$email.'")');
  }

  public static function delete_user($id)
  {  
    return mysql_query('DELETE FROM leden WHERE ID="'.$id.'"');
  }

  public static function edit_user($id, $firstname, $lastname, $email)
  { 
    if (! empty($firstname))
      mysql_query('UPDATE leden SET voornaam="'.$firstname.'" WHERE ID="'.$id.'"');

    if (! empty($lastname))
      mysql_query('UPDATE leden SET achternaam="'.$lastname.'" WHERE ID="'.$id.'"');

    if (! empty($email))
      mysql_query('UPDATE leden SET email="'.$email.'" WHERE ID="'.$id.'"');

    return true;
  }

  public static function get_user_count()
  {      
    $query  = "SELECT COUNT(ID) FROM leden";
    $result = mysql_query($query);
    $row    = mysql_fetch_assoc($result);

    return $row['COUNT(ID)'];
  }

  public static function selection_all($label, $name, $current = '', $in = false, $not_in = false)
  {
    $users = User::get_all();

    if (is_array($in))
      if (empty($in))
        return "<div class='full'>Hier zijn nog geen leden</div>";
      else
        $users = array_intersect($users, $in);

    if (is_array($not_in))
      $users = array_diff($users, $not_in);

    $html = "<div class='full'><div class='half'><select name='$name'><option>$label</option>";


    foreach ($users as $user)
    {
      $selected = (string) $user === $current ? 'selected' : '';
      $html .= sprintf('<option %s value="%d">%s %s</option>', $selected, $user->ID, $user->firstname, $user->lastname);
    }
   
    return $html . "</select></div><div class='half'><label>Persoon in de lijst vinden</label><input type='text' class='invoke_search'></div></div>";
  }

  public static function list_all($users)
  {
    $html = "<ol>";

    foreach ($users as $id)
    {
      $user = User::by_id($id);
      $html .= "<li>$user->firstname $user->lastname</li>";
    }

    echo $html . "</ol>";
  }

  public function __toString()
  {
    return $this->ID;
  }
}
?>